<?php
// This page is used to retrieve url information from alexa server


// Format example:
// show_category.php?q=usc


require_once("alexa_info.php");
require_once("google_search.php");
require_once("category_tree.php");


// determine request type
// url array need to be retrieve 	
$url_array = google_search($_GET['q']);
$category_tree = new category_tree();
$index=0;

foreach($url_array as $url){
	$alexa = new alexa_info($url['url']);
	$alexa->make_URL_info_request("UrlInfo","Related");
	$url_info = $alexa->get_result_array();	
	$category = $url_info['categories'][0];
	if($category['AbsolutePath']==""){
		$category_tree->add_list("Others",$index);
	}
	else
		$category_tree->add_list($category['AbsolutePath'],$index);

	$index++;
}


$arr = category_tree::find_category_url($category_tree,8);
$final_category = array();

foreach($arr as $cate){
	array_push($final_category,array("category_name"=>$cate->category_name, "url_list"=>$cate->url_list));
//	echo $cate->category_name.": ".$cate->url_count." ";
//	print_r($cate->url_list);
//	echo "<br/>";
}


// ajax send back
echo json_encode($final_category);

?>